package dichotomy;

public class BSNearLeft {
    /**
     * 查找数组 sortedArr 中大于等于 value 的最左位置
     * 
     * @param sortedArr
     * @param value
     * @return
     */
    public static int nearestLest(int[] sortedArr, int value) {
        if (sortedArr == null || sortedArr.length == 0) {
            return -1;
        }
        int L = 0;//
        int R = sortedArr.length - 1;
        int index = -1; // 记录最左的下标；
        while (L <= R) {
            int mid = L + ((R - L) >> 1);
            if (sortedArr[mid] >= value) {
                index = mid;
                R = mid - 1;
            } else {
                L = mid + 1;
            }
        }
        return index;
    }

    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,6,7,7,7,8,9};
        int index = nearestLest(arr, 7);
        System.out.println(index);

    }
}
